package com.cskaoyan.controller;

import com.cskaoyan.bean.Category;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.service.MarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    MarketService marketService;
    @RequestMapping("list")
    public BaseRespVo getCategoryList(){
        List<Category> categoryList = marketService.getCategoryList();
        return BaseRespVo.ok(categoryList);
    }
    @RequestMapping("l1")
    public BaseRespVo getCategoryListL1(){
        List<Map> categoryL1 = marketService.getCategoryL1();
        return BaseRespVo.ok(categoryL1);
    }
    @RequestMapping("create")
    public BaseRespVo categoryCreate(@RequestBody Category category){
        category.setDeleted(false);
        Category category1 = marketService.categoryCreate(category);
        return BaseRespVo.ok(category1);
    }
    @RequestMapping("update")
    public BaseRespVo categoryUpdate(@RequestBody Category category){
        BaseRespVo baseRespVo = new BaseRespVo();
        int i = marketService.categoryUpdate(category);
        if (i != 1){
            baseRespVo.setErrno(1);
            baseRespVo.setErrmsg("创建失败");
        }
        return BaseRespVo.ok(category);
    }
    @RequestMapping("delete")
    public BaseRespVo categoryDelete(@RequestBody Category category){
        BaseRespVo baseRespVo = new BaseRespVo();
        Integer i = marketService.categoryDelete(category);
        if (i != 1){
            baseRespVo.setErrno(1);
            baseRespVo.setErrmsg("删除失败");
        }
        return  BaseRespVo.ok();
    }
}
